// src/main.c

#include <stdio.h>
#include <unistd.h> // 用于 sleep()
#include <conio.h>
#include "snake.h"
#include "food.h"
#include "utils.h"
int main() {
    Snake snake;
    Food food;
    Direction newDir;
    int score = 0;

    initUtils();
    initSnake(&snake);
    initFood(&food, &snake);

    renderInitialGame(&snake, &food);
    while (1) {
        renderGame(&snake, &food, score);

        if (kbhit()) { // 如果有键盘输入
            newDir = getInput();

            // 确保不会反向移动
            if ((newDir == UP && snake.dir != DOWN) ||
                (newDir == DOWN && snake.dir != UP) ||
                (newDir == LEFT && snake.dir != RIGHT) ||
                (newDir == RIGHT && snake.dir != LEFT)) {
                snake.dir = newDir;
            }
        }

        moveSnake(&snake);

        if (checkCollision(&snake)) {
            printf("游戏结束！您的得分是：%d\n", score);
            break;
        }

        if (isFoodEaten(&food, &snake)) {
            score += 10;
            snake.length++;
            initFood(&food, &snake);
        }

        usleep(300000); // 暂停0.2秒，也可以根据需要调整
    }

    return 0;
}
